<template>
  <el-dialog title="新建资源"
    :visible="dialogVisible"
    width="600px"
    @close="close"
    :append-to-body="appendToBody">
    <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="资源名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="reset('ruleForm')">取 消</el-button>
      <el-button type="primary" @click="save('ruleForm')" :loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      ruleForm: {
        name: '新建文件夹'
      },
      loading: false,
    }
  },
  props: {
    dialogVisible: {
      type: Boolean
    },
    appendToBody: {
      type: Boolean,
      default: false
    },
    path: {
      type: String
    },
    node: {
      type: Object
    }
  },
  methods: {
    close () {
      this.$refs.ruleForm.resetFields();
      this.$emit('update:dialogVisible', false)
    },
    open () {
      console.log(this.path)
      this.$emit('update:dialogVisible', true)
    },
    save(formName) { // 新增
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$API({
            name: 'AddResource',
            params: {
              path: this.path,
            },
            headers: {
              'Content-Type': 'text/plain'
            },
            data: this.ruleForm.name,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.msg)
              this.close()
              if (this.appendToBody === true) {
                this.$emit('refresh', this.node)
              } else {
                this.$emit('getAll')
              }
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    reset(formName) {
      this.$refs[formName].resetFields();
      this.$emit('update:dialogVisible', false)
    },
  }
}
</script>

<style>

</style>